package com.ilove.weather;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class SMSender implements Runnable {
	private String message;
	private String phoneNumber;

	public SMSender(String message, String phoneNumber) {
		this.message = message;
		this.phoneNumber = phoneNumber;
	}

	public void send() {
		Thread t = new Thread(this);
		t.start();
	}

	public void run() {
		StringBuffer addr = new StringBuffer();
		addr.append("sms://+");
		addr.append(phoneNumber);
		String address = addr.toString();
		MessageConnection smsconn = null;
		try {
			// Open the message connection.
			smsconn = (MessageConnection) Connector.open(address);
			// Create the message.
			TextMessage txtmessage = (TextMessage) smsconn.newMessage(MessageConnection.TEXT_MESSAGE);
			txtmessage.setAddress(address);// !!
			txtmessage.setPayloadText(message);
			smsconn.send(txtmessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (smsconn != null) {
			try {
				smsconn.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}
}
